package com.example.benyq.mediaplayerdemo.recyclerview;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.benyq.mediaplayerdemo.glide.GlideCircleTransform;

/**
 * @author benyq
 * @date 18-5-20 18:54
 * @E-mail： 1520063035@qq.com
 * @description RecyclerHolder 1.0
 */
public class BenyqRVHolder extends RecyclerView.ViewHolder {

    private SparseArray<View> mViews = new SparseArray<>();

    private View mConvertView;
    private int mLayoutId;
    protected Context mContext;


    public BenyqRVHolder(Context context, View itemView) {
        super(itemView);
        mConvertView = itemView;
        mContext = context;
    }

    public <V extends View> V getView(int viewId) {
        View view = mViews.get(viewId);
        if (view == null) {
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (V) view;
    }


    public BenyqRVHolder setText(int viewId, String value) {
        TextView view = getView(viewId);
        view.setText(value);
        return this;
    }

    public BenyqRVHolder setImageUrl(int viewId, String imgUrl) {
        ImageView view = getView(viewId);
        Glide.with(mContext).load(imgUrl).into(view);
        return this;
    }

    public BenyqRVHolder setCircleImageUrl(int viewId, String imgUrl) {
        ImageView view = getView(viewId);
        Glide.with(mContext).load(imgUrl).transform(new GlideCircleTransform(mContext)).into(view);
        return this;
    }
}
